/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ericsson.nfc.signing.certificate;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

/**
 *
 * @author emarkki
 */
public class Files {
    
    public static final String NORDIC_ROOT_CA = "files/NordicRootCA.cer";
    public static final String MORA_NISSE = "files/MoraNisse.cer";
    public static final String DALARNA_CA = "files/DalarnaCA.cer";
    public static final String DALATRAFIK_CA = "files/DalatrafikCA.cer";
    public static final String LARS_NILS = "files/LarsNils.cer";
    
    public static final String SCANDINAVIA_ROOT_CA = "files/ScandinaviaRootCA.cer";
    public static final String NORRLAND_CA = "files/norrland-signed_by-scandinavia-root.cer";
    public static final String KIRUNA_MINING_CA = "files/kiruna-mining-signed_by-norrland.cer";
    
    public static final String MARKUS1 = "files/markus1-pub.cer";
    
    public static final String USER_NO_SIGN_RIGHT = "files/UserNoSignRight.cer";

    public static final String FALSE_CA = "files/FalseCA.cer";
    public static final String FALSE_USER_1 = "files/FalseUser1.cer";
    public static final String USER_CRITICAL_EXTENSION_1 = "files/UserCritExt1.cer";
    public static final String GOLD_CA = "files/GoldCA.cer";

    /** Not valid due to too long chain and pathlen contstraint in Gold CA */
    public static final String SILVER_CA = "files/SilverCA.cer";

    /** Not valid due to too long chain and pathlen contstraint in Gold CA */
    public static final String SILVER_USER_1 = "files/SilverUser1.cer";

    public static final String JAN_ERIK = "files/JanErik.cer";
    public static final String NORRBOTTEN_CA = "files/NorrbottenCA.cer";

    public static final String B_ROOT_CA = "files/b-root-ca.cer";
    public static final String B_USER2 = "files/b-user2.cer";

    private Hashtable data = new Hashtable();
    
    byte[] getBytes(String dataFile) {
        
        byte[] bytes = (byte[])data.get(dataFile);
        
        if(bytes == null) {
            bytes = readBinaryFile(dataFile);
            data.put(dataFile, bytes);
        }
        
        return bytes;
    }
    
    private byte[] readBinaryFile(String fileName) {
        try {

            
            InputStream in = getClass().getResourceAsStream(fileName);
            
            if(in == null) {
                throw new RuntimeException("No such file: " + fileName); // Note: Do not obfuscate this class!
            }
            
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

            byte[] buffer = new byte[512];

            int ch;
            while ((ch = in.read(buffer)) > 0) {
                out.write(buffer, 0, ch);
            }
            in.close();

            return out.toByteArray();
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new RuntimeException("IOException: " + ex.getMessage());
        }
    }
}
